<?php
class Event extends Ecore_Db {
	var $cache;
	var $clear;
	public function __construct() {
		$this->cache = new Ecore_Cache();
		$this->clear = $this->cache->clear;
	}		
	
	public function getAll($intAdtype, $intCategoryId, $intStatus, $intOption, $strKey, $strStart, $strEnd, $intOff, $intLimit, &$intTotal) {
		try {				
			$this->prepare('call events_getall(?,?,?,?,?,
											   ?,?,?,?)');		
			$this->bindParam(1, $intAdtype, PDO::PARAM_INT, 4);
			$this->bindParam(2, $intCategoryId, PDO::PARAM_INT, 11);
			$this->bindParam(3, $intStatus, PDO::PARAM_INT, 4);
			$this->bindParam(4, $intOption, PDO::PARAM_INT, 4);
			$this->bindParam(5, $strKey, PDO::PARAM_STR, 50);
			$this->bindParam(6, $strStart, PDO::PARAM_STR, 10);
			$this->bindParam(7, $strEnd, PDO::PARAM_STR, 10);
			$this->bindParam(8, $intOff, PDO::PARAM_INT, 11);
			$this->bindParam(9, $intLimit, PDO::PARAM_INT, 4);
			$arrResult 	= $this->fetchAll();
			$intTotal	= isset($arrResult[0]['foundRows']) ? $arrResult[0]['foundRows'] : 0;
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Ecore_Function::logDb('Backend::Event::getAll - ' .$e->getMessage());			
			
		}
	}
	public function get($intEventId) {
		try {						
			$this->prepare('call events_get(?)');		
			$this->bindParam(1, $intEventId, PDO::PARAM_INT, 11);
			$arrResult 	= $this->fetchAll();
			return isset($arrResult[0]) ? $arrResult[0] : null;
		} catch(Exception $e) {
			Ecore_Function::logDb('Backend::Event::get - ' .$e->getMessage());			
			
		}
	}
	public function add($intAdminId, $intUserId, $strName, $intCategoryId, $intSubId,
						$strAddr, $intDistrict, $intProvince, $strCountry, $dblLng,
						$dblLat, $strDstart, $strDend, $intAfrom, $intAto,
						$intQty, $intQtyReg, $strImage, $strShort, $strContent,
						$strTag, $intAdv, $strContact, $intPrice, $intAlbumId=0,
						$strFeatures='',$intAlbumType=0) {
		try {
			$this->prepare('call events_add(?,?,?,?,?,
										    ?,?,?,?,?,
										    ?,?,?,?,?,
										    ?,?,?,?,?,
										    ?,?,?,?,?,
											?,?
										)');
			$this->bindParam(1 , $intAdminId		, PDO::PARAM_INT, 11);
			$this->bindParam(2 , $intUserId			, PDO::PARAM_INT, 11);
			$this->bindParam(3 , $strName			, PDO::PARAM_STR, 255);
			$this->bindParam(4 , $intCategoryId		, PDO::PARAM_INT, 11);
			$this->bindParam(5 , $intSubId			, PDO::PARAM_INT, 11);
			$this->bindParam(6 , $strAddr			, PDO::PARAM_STR, 150);
			$this->bindParam(7 , $intDistrict		, PDO::PARAM_INT, 11);
			$this->bindParam(8 , $intProvince		, PDO::PARAM_INT, 11);
			$this->bindParam(9 , $strCountry		, PDO::PARAM_STR, 2);
			$this->bindParam(10, $dblLng			);
			$this->bindParam(11, $dblLat			);
			$this->bindParam(12, $strDstart			, PDO::PARAM_STR, 20);
			$this->bindParam(13, $strDend			, PDO::PARAM_STR, 20);
			$this->bindParam(14, $intAfrom			, PDO::PARAM_INT, 4);
			$this->bindParam(15, $intAto			, PDO::PARAM_INT, 4);
			$this->bindParam(16, $intQty			, PDO::PARAM_INT, 6);
			$this->bindParam(17, $intQtyReg			, PDO::PARAM_INT, 6);
			$this->bindParam(18, $strImage			, PDO::PARAM_STR, 150);
			$this->bindParam(19, $strShort			, PDO::PARAM_STR, 255);
			$this->bindParam(20, $strContent		, PDO::PARAM_STR);
			$this->bindParam(21, $strTag			, PDO::PARAM_STR, 255);
			$this->bindParam(22, $intAdv			, PDO::PARAM_INT, 4);
			$this->bindParam(23, $strContact		, PDO::PARAM_STR);
			$this->bindParam(24, $intPrice			, PDO::PARAM_INT, 11);
			$this->bindParam(25, $strFeatures		, PDO::PARAM_STR);
			$this->bindParam(26, $intAlbumId		, PDO::PARAM_INT, 11);			
			$this->bindParam(27, $intAlbumType		, PDO::PARAM_INT, 4);
			$arrResult 		= $this->fetchAll();		
			$intError		= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			$intEventId 	= isset($arrResult[0]['eventid']) ? $arrResult[0]['eventid'] : 0;
			if($intError == 0) {				
				$objSearch		= new Ecore_Search();
				$objCommon		= new Ecore_Common();
				$arrProvince	= $objCommon->getProvince($strCountry);			
				$arrDistrict	= $objCommon->getDistrict($intProvince);	
				$strAddr		= $strAddr. ', ' . $arrDistrict[$intDistrict] . ', ' . $arrProvince[$intProvince];
				$strSlug		= Ecore_Rewrite_Event::slug($intEventId,$strName);
				$objSearch->addUpdate($intEventId, EVENT_TYPE, $strName, $strAddr, $strTag,
									  $strShort, $strImage, $strSlug, $dblLng, $dblLat,
									  Ecore_Function::now(), 1, 3, $intCategoryId, $intSubId);
			}
			return $intError;
		} catch(Exception $e) {
			Ecore_Function::logDb('Backend::Event::add - ' . $e->getMessage());
			
		}
	}		
	public function update($intAdminId, $intEventId, $intUserId, $strName, $intCategoryId, 
							$intSubId, $strAddr, $intDistrict, $intProvince, $strCountry, 
							$dblLng, $dblLat, $strDstart, $strDend, $intAfrom, 
							$intAto, $intQty, $intQtyReg, $strImage, $strShort, 
							$strContent, $strTag, $intAdv, $strContact, $intPrice, 
							$intAlbumId=0,$strFeatures='',$intAlbumType=0) {
		try {
			$this->prepare('call events_update(?,?,?,?,?,
											   ?,?,?,?,?,
											   ?,?,?,?,?,
											   ?,?,?,?,?,
											   ?,?,?,?,?,
											   ?,?,?)');
			$this->bindParam(1 , $intAdminId		, PDO::PARAM_INT, 11);
			$this->bindParam(2 , $intEventId		, PDO::PARAM_INT, 11);
			$this->bindParam(3 , $intUserId			, PDO::PARAM_INT, 11);
			$this->bindParam(4 , $strName			, PDO::PARAM_STR, 255);
			$this->bindParam(5 , $intCategoryId		, PDO::PARAM_INT, 11);
			$this->bindParam(6 , $intSubId			, PDO::PARAM_INT, 11);
			$this->bindParam(7 , $strAddr			, PDO::PARAM_STR, 150);
			$this->bindParam(8 , $intDistrict		, PDO::PARAM_INT, 11);
			$this->bindParam(9 , $intProvince		, PDO::PARAM_INT, 11);
			$this->bindParam(10, $strCountry		, PDO::PARAM_STR, 2);
			$this->bindParam(11, $dblLng			);
			$this->bindParam(12, $dblLat			);
			$this->bindParam(13, $strDstart			, PDO::PARAM_STR, 20);
			$this->bindParam(14, $strDend			, PDO::PARAM_STR, 20);
			$this->bindParam(15, $intAfrom			, PDO::PARAM_INT, 4);
			$this->bindParam(16, $intAto			, PDO::PARAM_INT, 4);
			$this->bindParam(17, $intQty			, PDO::PARAM_INT, 6);
			$this->bindParam(18, $intQtyReg			, PDO::PARAM_INT, 6);
			$this->bindParam(19, $strImage			, PDO::PARAM_STR, 150);
			$this->bindParam(20, $strShort			, PDO::PARAM_STR, 255);
			$this->bindParam(21, $strContent		, PDO::PARAM_STR);
			$this->bindParam(22, $strTag			, PDO::PARAM_STR, 255);
			$this->bindParam(23, $intAdv			, PDO::PARAM_INT, 1);
			$this->bindParam(24, $strContact		, PDO::PARAM_STR);
			$this->bindParam(25, $intPrice			, PDO::PARAM_INT, 11);
			$this->bindParam(26, $strFeatures		, PDO::PARAM_STR);
			$this->bindParam(27, $intAlbumId		, PDO::PARAM_INT, 11);			
			$this->bindParam(28, $intAlbumType		, PDO::PARAM_INT, 4);
			$arrResult 	= $this->fetchAll();		
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {				
				$strOldImage = isset($arrResult[0]['image']) ? $arrResult[0]['image'] : '';
				if($strImage != '') {
					$this->deleteImg($strOldImage);
				}
				$objSearch		= new Ecore_Search();
				$objCommon		= new Ecore_Common();
				$arrProvince	= $objCommon->getProvince($strCountry);			
				$arrDistrict	= $objCommon->getDistrict($intProvince);	
				$strAddr		= $strAddr. ', ' . $arrDistrict[$intDistrict] . ', ' . $arrProvince[$intProvince];
				$strSlug		= Ecore_Rewrite_Event::slug($intEventId,$strName);
				$objSearch->addUpdate($intEventId, EVENT_TYPE, $strName, $strAddr, $strTag,
									  $strShort, $strImage, $strSlug, $dblLng, $dblLat,
									  Ecore_Function::now(), 1, 3, $intCategoryId, $intSubId);
			}
			return $intError;
		} catch(Exception $e) {
			Ecore_Function::logDb('Backend::Event::update - ' . $e->getMessage());
			
		}
	}	
	public function updateStatus($intAdminId, $intEventId, $intStatus, $intPerm) {
		try {				
			$this->prepare('call events_update_status(?,?,?,?)');
			$this->bindParam(1, $intAdminId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intEventId, PDO::PARAM_INT, 11);
			$this->bindParam(3, $intStatus, PDO::PARAM_INT, 4);
			$this->bindParam(4, $intPerm, PDO::PARAM_INT, 4);
			$arrResult 	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				$objSearch = new Ecore_Search();
				$objSearch->updateStatus($intEventId, EVENT_TYPE, $intStatus);
			}
			return $intError;
		} catch(Exception $e) {
			Ecore_Function::logDb('Backend::Event::updateStatus - ' .$e->getMessage());			
			
		}
	}
	public function delete($intAdminId, $intEventId) {
		try {			
			$this->prepare('call events_delete(?,?)');
			$this->bindParam(1, $intAdminId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intEventId, PDO::PARAM_INT, 11);
			$arrResult 	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {				
				$strImage = isset($arrResult[0]['image']) ? $arrResult[0]['image'] : '';
				if($strImage != '') {
					$this->deleteImg($strImage);
				}
			}
			return $intError;
		} catch(Exception $e) {
			Ecore_Function::logDb('Backend::Event::delete - ' .$e->getMessage());			
			
		}
	}
	public function deleteImg($strImg) {
		if(file_exists(DIR_EVENT_FULL . $strImg))
			@unlink(DIR_EVENT_FULL . $strImg);
		if(file_exists(DIR_EVENT_THUMB . $strImg))
			@unlink(DIR_EVENT_THUMB . $strImg);
		if(file_exists(DIR_EVENT_SMALL . $strImg))
			@unlink(DIR_EVENT_SMALL . $strImg);
	}
}